//
//  RYChatManager.m
//  SanYiTeaching
//
//  Created by ChenHao on 2019/8/1.
//  Copyright © 2019 SanYi. All rights reserved.
//

#import "RYChatManager.h"
#import "AppDelegate.h"
#import "ChatClassModel.h"

@implementation RYChatManager{
    NSMutableArray *dataSoure;
}

- (instancetype)init{
    if (self = [super init]) {
        [RCIM sharedRCIM].userInfoDataSource = self;
    }
    return self;
}

+ (RYChatManager *)shareManager{
    static RYChatManager* manager = nil;
    static dispatch_once_t predicate;
    dispatch_once(&predicate, ^{
        manager = [[[self class] alloc] init];
    });
    return manager;
}
/**
 *  从服务器同步好友列表
 */
-(void)syncFriendList:(void (^)(NSMutableArray* friends,BOOL isSuccess))completion
{
    dataSoure = [[NSMutableArray alloc]init];
    NSDictionary *dict = @{@"keyWord":@""};
    [SYRequestAPI postDataFromURLScheme:@"rong/RongService.svc/GetTeacherMailList" parameterDictionary:dict success:^(id responseObject) {
        NSArray *arr = responseObject[@"data"];
        AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];

        [appDelegate.AllArray addObjectsFromArray:[ChatClassModel mj_objectArrayWithKeyValuesArray:arr]];
        for (ChatClassModel *classModel in appDelegate.AllArray) {
            classModel.isShow = @"0";
            for (ChatUserModel *userModle in classModel.ListStudent) {
                RCUserInfo *aUserInfo = [[RCUserInfo alloc]initWithUserId:userModle.imid name:userModle.name portrait:userModle.headPath];
                [self->dataSoure addObject:aUserInfo];

            }
        }
        appDelegate.friendsArray = self->dataSoure;
        completion(self->dataSoure,YES);
        
    } fail:^(NSError *error) {
        
    }];

    
}
#pragma mark
#pragma mark - RCIMUserInfoDataSource
- (void)getUserInfoWithUserId:(NSString*)userId completion:(void (^)(RCUserInfo*))completion
{
    NSLog(@"getUserInfoWithUserId ----- %@", userId);
    
    dispatch_async(dispatch_get_main_queue(), ^{
        AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
        
        if (userId == nil || [userId length] == 0 )
        {
            [self syncFriendList:^(NSMutableArray *friends, BOOL isSuccess) {
                
            }];
            
            completion(nil);
            return ;
        }
        
        if ([userId isEqualToString:[RCIM sharedRCIM].currentUserInfo.userId]) {
            RCUserInfo *myselfInfo = [[RCUserInfo alloc]initWithUserId:[RCIM sharedRCIM].currentUserInfo.userId name:[RCIM sharedRCIM].currentUserInfo.name portrait:[RCIM sharedRCIM].currentUserInfo.portraitUri];
            completion(myselfInfo);
            
        }
        
        for (NSInteger i = 0; i<appDelegate.friendsArray.count; i++) {
            RCUserInfo *aUser = appDelegate.friendsArray[i];
            if ([userId isEqualToString:aUser.userId]) {
                completion(aUser);
                break;
            }
        }
    });
    
    
}
#pragma mark
#pragma mark - RCIMGroupInfoDataSource

-(void)loginRongCloudWithUserInfo:(RCUserInfo *)userInfo withToken:(NSString *)token{
    

        //同步好友列表
        [self syncFriendList:^(NSMutableArray *friends, BOOL isSuccess) {
            NSLog(@"%@",friends);
            if (isSuccess) {
                NSLog(@" success 发送通知");
                
            }
        }];
    [RCIMClient sharedRCIMClient].currentUserInfo = userInfo;

    
}

#pragma mark
#pragma mark 根据userId获取RCUserInfo
-(RCUserInfo *)currentUserInfoWithUserId:(NSString *)userId{
    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];

    for (NSInteger i = 0; i<appDelegate.friendsArray.count; i++) {
        RCUserInfo *aUser = appDelegate.friendsArray[i];
        if ([userId isEqualToString:aUser.userId]) {
            NSLog(@"current ＝ %@",aUser.name);
            return aUser;
        }
    }
    return nil;
}
@end
